package com.hiray.demos.rv_related

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.DiffUtil
import androidx.appcompat.widget.AppCompatTextView
import androidx.recyclerview.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import com.hiray.demos.ItemAdapter
import com.hiray.demos.R
import kotlinx.android.synthetic.main.activity_diff_util.*

class DiffUtilActivity : AppCompatActivity(), ItemAdapter.OnItemClickListener {

    override fun onItemClick(position: Int) {

    }

    val items = arrayListOf<TextItem>()

    private val mAdapter = TextAdapter()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_diff_util)
        loadItems()
        recyclerview.adapter = mAdapter
    }

    private fun loadItems() {
        for (i in 0..25)
            items.add(TextItem(i, "Content $i"))

        var calculateDiff = DiffUtil.calculateDiff(DiffCallBack(null, items))
        calculateDiff.dispatchUpdatesTo(mAdapter)
    }

    fun changeItem(view: View) {
       //no op
    }

    fun addItem(view: View) {
        val new = arrayListOf<TextItem>()
        new.addAll(items)
        new.add(TextItem(25,"Content new added hellos"))
        val calculateDiff = DiffUtil.calculateDiff(DiffCallBack(items, new))
        calculateDiff.dispatchUpdatesTo(mAdapter)
    }

    fun removeItem() {
        val new = arrayListOf<TextItem>()
        new.addAll(items)
        items.removeAt(3)
        val calculateDiff = DiffUtil.calculateDiff(DiffCallBack(new, items))
        calculateDiff.dispatchUpdatesTo(mAdapter)
    }

    inner class TextAdapter : androidx.recyclerview.widget.RecyclerView.Adapter<TextAdapter.ItemViewHolder>() {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {

            return ItemViewHolder(View.inflate(this@DiffUtilActivity, R.layout.demo_item, null))
        }

        override fun getItemCount(): Int {
            return items.size
        }

        override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
            holder.textView.text = items[position].content
        }

        inner class ItemViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(itemView) {
            var textView: AppCompatTextView = itemView.findViewById(R.id.item_name) as AppCompatTextView
        }
    }


    //最好定义成通用接口，而不是针对具体某个类，通用性不够
    inner class DiffCallBack(var oldItems: List<TextItem>?, var newItems: List<TextItem>?) : DiffUtil.Callback() {


        override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
            return oldItems!![oldItemPosition].id == newItems!![newItemPosition].id
        }

        override fun getOldListSize(): Int {
            return if (oldItems == null)
                0
            else oldItems!!.size
        }

        override fun getNewListSize(): Int {
            return if (newItems == null)
                0
            else newItems!!.size
        }

        override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
            return oldItems!![oldItemPosition].content == newItems!![newItemPosition].content
        }

    }


    class TextItem(var id: Int, var content: String)
}
